package 力扣._12整数转罗马数字;

class Solution {
    char[] c = new char[]{'I','V','X','L','C','D','M'};
    int[] v = new int[]{1,5,10,50,100,500,1000};
    int len = 7;

    String[] c2 = new String[]{"IV","IX","XL","XC","CD","CM"};
    int[] v2 = new int[]{4,9,40,90,400,900};
    int len2 = 6;

    int getFirst(int n){
        while(n>=10)
            n/=10;
        return n;
    }
    public String intToRoman(int num) {
        StringBuilder s = new StringBuilder();
        while(num>0){
            // 获取数字首位
            int n = getFirst(num);
            if(n!=4 && n!= 9){
                for(int i = len-1;i>=0;i--){
                    if(num >= v[i]){
                        s.append(c[i]);
                        num-=v[i];
                        break;
                    }
                }
            }else{
                for(int i = len2-1;i>=0;i-- ){
                    if(num >= v2[i]){
                        s.append(c2[i]);
                        num-=v2[i];
                        break;
                    }
                }
            }
        }
        return s.toString();
    }
}